"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

"""
"""
Case Type   : 资源池化
Case Name   : 资源池化场景下，无任何业务压力, 查询DSS IO统计信息
Create At   : 2024-04-07
Owner       : zhongchengzhi
Description :
    1、查看集群状态
    2、建立表格
    3、连接数据库并向表中插入大量数据
    4、连接数据库并查询指定时间间隔内的DSS IO信息
    5、连接数据库并向表中查询大量数据
    6、连接数据库并查询指定时间间隔内的DSS IO信息
    7、删除段页式表`viewTb`
    8、恢复集群原始状态
Expect      :
    1、集群状态正常
    2、建立表格成功
    3、集群状态正常，数据库节点状态正常
    4、数据库服务在给定时间内产生数据写入
    5、集群状态正常，数据库节点状态正常
    6、数据库服务在给定时间内产生数据读取
    7、删除段页式表'viewTb'成功
    8、恢复集群原始状态成功,集群状态正常
History     :
    1、2024-04-07 创建
"""
import os
import unittest
import time

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行;')
class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.s1_comsh = CommonSH('Standby1DbUser')

    def test_shared_storage(self):
        text = '-----step1:查看集群状态; expect:集群状态正常-----'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)

        text = '-----step2:建立表格; expect:建立表格成功-----'
        self.log.info(text)
        sql = f"create table viewTb_zcz(name text)with(segment=on);"
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('CREATE TABLE', status, '执行失败' + text)

        text = '-----step3:连接数据库并向表中插入大量数据; expect:集群状态正常，数据库节点状态正常-----'
        self.log.info(text)
        sql = f"insert into viewTb_zcz values (generate_series(0, 199999));"
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('INSERT 0 200000', status, '执行失败' + text)

        text = '-----step4:连接数据库并查询指定时间间隔内的DSS IO信息; expect:数据库服务在给定时间内产生数据写入-----'
        self.log.info(text)
        sql = f"select * from dss_io_stat(2);"
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('(1 row)', status, '执行失败' + text)

        text = '-----step5:连接数据库并向表中查询大量数据 expect:数据库服务在给定时间内产生数据读取-----'
        self.log.info(text)
        sql = f"select * from viewTb_zcz;"
        self.log.info(sql)
        sql_data = Primary_SH.execut_db_sql(sql)
        self.log.info(sql_data)
        self.assertIn('rows', sql_data, '执行失败' + text)


        text = '-----step6:连接数据库并查询指定时间间隔内的DSS IO信息; expect:数据库服务在给定时间内产生数据写入-----'
        self.log.info(text)
        sql = f"select * from dss_io_stat(2);"
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('(1 row)', status, '执行失败' + text)

    def tearDown(self):
        text = '-----step7:删除段页式表`viewTb` expect:删除表tb成功-----'
        self.log.info(text)
        sql = f"drop table viewTb_zcz;"
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('DROP TABLE', status, '执行失败' + text)

        text = '-----step8:恢复环境为初始集群状态; expect:成功-----'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        status = Primary_SH.check_cluster_core_status(status, wait_times=600)
        self.log.info(status)
        status = Primary_SH.exec_cm_ctl(mode='stop')
        self.log.info(status)
        self.assertIn('cm_ctl: stop cluster successfully.', status, '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='start')
        self.log.info(status)
        self.assertIn('cm_ctl: start cluster successfully.', status, '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='switchover', param='-a')
        self.log.info(status)
        self.assertIn(self.constant.cm_switchover_success_msg, status, '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')